package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-demo/service"
	"go-demo/util"
)

type DemoController struct {
	DemoAppService *service.DemoAppService
}

func NewDemoController(demoAppService *service.DemoAppService) *DemoController {
	return &DemoController{
		DemoAppService: demoAppService,
	}
}

func (ctl *DemoController) GetOne(ctx *gin.Context) {
	id := ctx.Param("id")
	util.JsonWithCode(ctx, fmt.Sprintf("hello %s", id), util.StatusOk, nil)
}

// 获取列表demo
// @Summary 获取列表demo
// @Description 获取列表demo
// @Tags demo
// @Accept  json
// @Produce  json
// @Param  pageIndex	 	query 	number 	 false 	"页码idx"
// @Param  pageSize  	 	query 	number 	 false 	"页容量"
// @Param  billNo  	 	    query 	string 	 false 	"单号"
// @Param  itemNo  	 	    query 	string 	 false	"行号"
// @Success 200 {array} service.DemoDto
// @Router /Demo [get]
func (ctl *DemoController) GetList(ctx *gin.Context) {
	var input service.GetListDemoInput
	if err := ctx.ShouldBindQuery(&input); err != nil {
		util.Json(ctx, nil, err)
		return
	}
	pageResponse, code, err := ctl.DemoAppService.GetList(ctx, &input)
	util.JsonWithCode(ctx, pageResponse, code, err)
}

// 创建一个demo
// @Summary 创建一个demo
// @Description 创建一个demo
// @Tags demo
// @Accept  json
// @Produce  json
// @Param	body	body 	service.CreateDemoInput	true	"body for 添加"
// @Success 200 {object} service.DemoDto
// @Router /Demo [post]
func (ctl *DemoController) Create(ctx *gin.Context) {
	var input *service.CreateDemoInput
	if err := ctx.ShouldBindJSON(&input); err != nil {
		util.Json(ctx, nil, err)
		return
	}
	demoDto, code, err := ctl.DemoAppService.Create(ctx, input)
	util.JsonWithCode(ctx, demoDto, code, err)
}

// 删除一个Demo
// @Summary 删除一个Demo
// @Description 删除一个Demo
// @Tags demo
// @Accept  json
// @Produce  json
// @Param  id	 	query 	string 	 true 	"id"
// @Success 200 {object} service.DemoDto
// @Router /Demo/{id} [delete]
func (ctl *DemoController) Delete(ctx *gin.Context) {
	id := ctx.Param("id")
	code, err := ctl.DemoAppService.Delete(ctx, id)
	util.JsonWithCode(ctx, nil, code, err)
}
